
##########################################################################
#
# Variables
#

top		= .
src     = ./src
test    = ./test
hsmodules	= $(shell find $(src) -name "*.hs")
lhsmodules	= $(shell find $(src) -name "*.lhs")
modules		= $(shell find $(src) -name "*.hs" -or -name "*.lhs")

contrib		= ./contrib

###########################################################################
#
# Instructions
#

all:
	@cat instructions.txt

###########################################################################
#
# GHC
#

ghcPath		= $(src):$(test):$(contrib)/HUnit-1.0

ghcOpts		= -fwarn-overlapping-patterns -fwarn-deprecations \
		  -fwarn-duplicate-exports -fwarn-missing-fields \
		  -fwarn-unused-imports \
		  -w \
		  -i$(ghcPath)

ghcCmd		= ghc -hidir hidir -odir odir ${ghcOpts}

outdirs:
	mkdir -p hidir
	mkdir -p odir

ghc: outdirs
	$(ghcCmd) --make $(modules)

ghci: outdirs
	$(ghcCmd) --interactive $(modules)

###########################################################################
#
# HUnit
#

huModules	= $(shell find $(test) -name "*HUnit.hs" -or \
                                   -name "*HUnit.lhs")

huSession = session.hunit

hunit: ghc
	@echo "Running HUnit on modules: $(huModules)"
	@echo "" > $(huSession)
	@$(MAKE) $(patsubst %, %.hunit, $(huModules))
	@ghci -hidir hidir -odir odir -w $(ghcOpts) -v0 < $(huSession)
	@rm $(huSession)

%.hunit:
	@echo ":l $*" >>  $(huSession)
	@echo "main" >> $(huSession)

###########################################################################
#
# Cleaning

hiModules	= hidir 
oModules	= odir 

clean:
	$(RM) -rf $(hiModules) $(oModules) $(covdir) *HUnit* module-graph.dot 

###########################################################################
#
# Generating module graph

nodes = $(subst /,\., $(subst ./src/,\", $(subst .hs,\", $(modules))))

graphviz: module-graph.dot

module-graph.dot: $(modules)
	@echo "digraph M {" > $@
	@echo "node [shape=box]" >> $@
	@echo "subgraph clusterNodes {" >> $@
	@echo "node [style=filled,color=lightblue]" >> $@
	@echo $(nodes) >> $@
	@echo "}" >> $@
	@grep import $(modules) \
	| grep -v "Data.Set" \
	| grep -v "Data.List" \
	| grep -v "Data.Maybe" \
	| grep -v "Data.Map" \
	| grep -v "Control.Monad" \
	| sed "s/hiding //g" \
	| sed "s/import qualified/import/g" \
	| sed "s/.hs:import /\" -> \"/g" \
	| sed "s#\./src/#\"#g" \
	| sed "s#/#\.#g" \
	| sed "s/as\ .*$$//g" \
	| sed "s/(.*)//g" \
	| sed "s/\ *$$/\"/g" \
	>> $@
	@echo "}" >> $@
	@echo "Module graph written to: $@"

###########################################################################
#
#Coverage 
#
hunitModules	= $(subst .lhs,,$(subst .hs,,$(subst $(contrib)/HUnit-1.0/,--exclude=,$(shell find $(contrib) -name "*HUnit*.hs" -or -name "HUnit*.lhs"))))

run-coverage: covdirs $(huModules:.hs=.runcoverage) 

covdirs:
	mkdir -p report
	mkdir -p report/general/
	mkdir -p report/general/html/
	mkdir -p report/odir
	mkdir -p report/hidir
	mkdir -p report/hpcdir

%.runcoverage: %.hs
	$(MAKE) covmodule=$* $*.runcov

covmod = $(subst /,-,$(covmodule))
covdir = report/$(covmod)
covout = $(covdir)/output.txt
testmods =  $(subst .hs,,$(subst /,.,$(subst ./test/,--exclude=,$(huModules))))
covmain = $(subst test.,,$(subst /,.,$(covmodule))).main

%.runcov: %.hs
	mkdir -p $(covdir)
	mkdir -p $(covdir)/html	
	@echo Starting Coverage report for $(covmod)
	@echo Compiling: $<
	ghc --make -fhpc -main-is $(covmain) $(ghcOpts) $< -odir report/odir -hidir report/hidir -o $(covmod) -hpcdir report/hpcdir
	@>$(covout)
	@echo ----------------------------->> $(covout)
	@echo --------Test output---------->> $(covout)
	@echo ----------------------------->> $(covout)
	@echo Executing.......
	@./$(covmod) >> $(covout)
	@echo ----------------------------->> $(covout)
	@echo --------hpc output----------->> $(covout)
	@echo ----------------------------->> $(covout)
	@echo Builiding report....
	@hpc report $(covmod) --hpcdir=report/hpcdir >> $(covout)
	@echo Builiding html....
	@hpc markup $(covmod) --hpcdir=report/hpcdir --destdir=$(covdir)/html/

coverage: run-coverage 
	@echo Merging reports....
	@hpc sum *.tix $(testmods) $(hunitModules) --union --output allHUnit.tix
	@echo Building general report.....
	@> report/general/output.txt
	@hpc report allHUnit.tix >> report/general/output.txt --hpcdir=report/hpcdir
	@hpc markup allHUnit.tix --fun-entry-count --destdir report/general/html/ --hpcdir report/hpcdir
	@rm *HUnit*
	@echo Done  :D


############################################################################
#
# Debugging this Makefile

echo-%:
	@echo $($(*))
	
#############################################################################
